;
(function(window) {
    var avlist,render = function(html, data, scope) {
            avlist = _o2avList(data);
            return compile.call(data,html).apply(data, avlist[1]);
        },
        openTag = "<%",
        closeTag = "%>",

        compile = function(source) {
            var outReg = new RegExp(openTag + "=", "g"),
                safeOutReg = new RegExp(openTag + "\:\=", "g"),
                openReg = new RegExp(openTag, "g"),
                codeArr = source.replace(/[\r\t\n]/g, "").split(closeTag),
                fnBody = " var $ = '';";
               this._encodeHTML = _encodeHTML;
            for (var i = 0; i < codeArr.length; i++) {
                if (codeArr[i].indexOf(openTag + "=") != -1) {
                    fnBody += ";$+='" + codeArr[i].replace(outReg, "'+");
                } else if (codeArr[i].indexOf(openTag + ":=") != -1) {
                    fnBody += ";$+='" + codeArr[i].replace(safeOutReg, "'+this._encodeHTML(")+")";
                } else if (codeArr[i].indexOf(openTag) != -1) {
                    fnBody += ";$+='" + codeArr[i].replace(openReg, "';");
                } else {
                    fnBody += ";$+='" + codeArr[i] + "';";
                }
            }
            return new Function(avlist[0].toString(),fnBody + " return $;");
        },
        _encodeHTML = function(source) {
            return String(source)
                .replace(/&/g, '&amp;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/\\/g, '&#92;')
                .replace(/"/g, '&quot;')
                .replace(/'/g, '&#39;');
        },
        _o2avList = function(obj){
            var ret = [[],[]];
            for(var k in obj){
                ret[0].push(k);
                ret[1].push(obj[k]);
            }
            return ret;
        };
    window.xtpl = {
        render: render,
        compile: compile
    }
})(window);